package module.building.view.produceweapon
{
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.LabelButton;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	
	import flash.events.MouseEvent;
	
	import module.building.model.dataObj.EquipObj;
	import module.main.view.ResourceConst;

	public class ProduceWeaponNeed extends UIComponent
	{
		/**
		 *装备所需背景 
		 */		
		private var _equipNeedBg:UIComponent;
		/**
		 *消耗 
		 */		
		private var _consumeLabel:Label;
		/**
		 *产出 
		 */		
		private var _productLabe:Label;
		private var _getExperienceLabel:Label;
		private var _equipNumLabel:Label;
		/**
		 *需要石块 
		 */		
		private var _stoneNeed:UIComponent;
		private var _stoneNeedLabel:Label;
		/**
		 *需要的铁块 
		 */		
		private var _ironNeed:UIComponent;
		private var _ironNeedNum:Label;
		/**
		 *需要木材 
		 */		
		private var _woodNeed:UIComponent;
		private var _woodNeedLabel:Label;
		/**
		 *需要的食物 
		 */		
		private var _goldNeed:UIComponent;
		private var _goldNeedLabel:Label;
		private var _kindOfEquip:UIComponent;
		private var _getExperience:UIComponent;
		/**
		 *装备配置 
		 */
		private var _equipArr:Array;
		public function ProduceWeaponNeed(arr:Array)
		{
			buttonArr = new Array();
			_equipArr = arr;
			initView();
		}
		/**
		 *用来存储button的一个数组 
		 */		
		private var buttonArr:Array;
		private function getButtonByTime(time:int,timeUint:String):LabelButton
		{
			var $produceTimeLButton:LabelButton = new LabelButton();
			$produceTimeLButton.backgroundSkin = "produce." + ResourceConst.EQUIP_PRODUC_NEED_TIME_BUTTON;
			$produceTimeLButton.width = 58;
			$produceTimeLButton.height = 23.9;
			$produceTimeLButton.text = time.toString() + timeUint;
			var text:Label = new Label();
			text.text = $produceTimeLButton.text;
			$produceTimeLButton.textX =($produceTimeLButton.width - text.width) / 2;
			$produceTimeLButton.textY =($produceTimeLButton.height - text.height) / 2;
			buttonArr.push($produceTimeLButton);
			return $produceTimeLButton;
		}
		private function initView():void
		{
			_equipNeedBg = new UIComponent;
			_equipNeedBg.backgroundSkin = "EditTroop." + ResourceConst.EDIT_TROOP_NAME_BACKGROUND;
			_equipNeedBg.width = 421.9;
			_equipNeedBg.height = 202.7;
			this.addChild(_equipNeedBg);
			
			_consumeLabel = TextFieldFactory.getInstance().getLabel("equipmentInterface_cost");
			_consumeLabel.text = "消耗";
			
			_productLabe = TextFieldFactory.getInstance().getLabel("equipmentInterface_output");
			_productLabe.text = "产出";
			
			_stoneNeed = new UIComponent();
			_stoneNeedLabel = new Label();
			_stoneNeed.backgroundSkin = "mainInterface." + ResourceConst.RESOURCEINFO_STONE_ICON;
			_stoneNeed.height = 21.9;
			_stoneNeed.width = 24.9;
			
			_woodNeed = new UIComponent();
			_woodNeedLabel = new Label();
			_woodNeed.backgroundSkin = ResourceConst.BUILD_ICON_WOOD;
			_woodNeed.height = 18.9;
			_woodNeed.width = 27.9;
				
			_goldNeed = new UIComponent();
			_goldNeedLabel = new Label();
			_goldNeed.backgroundSkin = "mainInterface." + ResourceConst.RESOURCEINFO_GOLD_ICON;
			_goldNeed.height = 24.9;
			_goldNeed.width = 23.9;
			
			_kindOfEquip = new UIComponent();
			
			_getExperience = new UIComponent();
			_getExperience.backgroundSkin = ResourceConst.GRADE_ICON;
			_getExperience.height = 33.0;
			_getExperience.width = 35.0;
			
			_getExperienceLabel = new Label();
			_equipNumLabel = new Label();
			
			_ironNeed = new UIComponent();
			_ironNeed.backgroundSkin = "mainInterface." + ResourceConst.RESOURCEINFO_IRON_ICON;
			_ironNeed.height = 18.9;
			_ironNeed.width = 23.9;
			_ironNeedNum = new Label();
			
			setButton();
			setDirection();
			setNeedBg();
			setProduceBg();
		}
		private function setNeedBg():void
		{
			var $needBg:UIComponent = new UIComponent();
			$needBg.backgroundSkin = "produce." + ResourceConst.EQUIP_PRODUCE_SMALL_BG;
			$needBg.width = 128;
			$needBg.height = 122;
			$needBg.x = 28;
			$needBg.y = 68;
			addChild($needBg);
			
			_stoneNeed.x = $needBg.x + 25;
			_stoneNeed.y = $needBg.y +7;
			_stoneNeedLabel.x = _stoneNeed.x + _stoneNeed.width + 5;
			_stoneNeedLabel.y = _stoneNeed.y;
			addChild(_stoneNeed);
			addChild(_stoneNeedLabel);
			
			_woodNeed.x = _stoneNeed.x;
			_woodNeed.y = _stoneNeed.y + _stoneNeed.height +7;
			_woodNeedLabel.x = _stoneNeedLabel.x;
			_woodNeedLabel.y = _woodNeed.y;
			addChild(_woodNeed);
			addChild(_woodNeedLabel);
			
			_goldNeed.x = _stoneNeed.x;
			_goldNeed.y = _woodNeed.y + _woodNeed.height +7;
			_goldNeedLabel.x = _stoneNeedLabel.x;
			_goldNeedLabel.y = _goldNeed.y;
			addChild(_goldNeed);
			addChild(_goldNeedLabel);
			
			_ironNeed.x = _stoneNeed.x;
			_ironNeed.y = _goldNeed.y + _goldNeed.height + 7;
			_ironNeedNum.x =_stoneNeedLabel.x;
			_ironNeedNum.y = _ironNeed.y;
			addChild(_ironNeed);
			
			_consumeLabel.x = 38;
			_consumeLabel.y = 45;
			addChild(_consumeLabel);
		}
		private function setDirection():void
		{
			var $direction:UIComponent = new UIComponent();
			$direction.backgroundSkin = "produce." + ResourceConst.EQUIP_DIRECTION_ICON;
			$direction.width = 81;
			$direction.height = 61;
			$direction.x = 170;
			$direction.y = 98;
			addChild($direction);
		}
		private function setProduceBg():void
		{
			var $produceBg:UIComponent = new UIComponent();
			$produceBg.backgroundSkin = "produce." + ResourceConst.EQUIP_PRODUCE_SMALL_BG;
			$produceBg.width = 128;
			$produceBg.height = 122;
			$produceBg.x = 270;
			$produceBg.y = 68;
			addChild($produceBg);
			
			_getExperience.x = 22;
			_getExperience.y = 70;
			$produceBg.addChild(_getExperience);
			
			_kindOfEquip.x = 22;
			_kindOfEquip.y = 22;
			$produceBg.addChild(_kindOfEquip);
			
			_productLabe.x = 280;
			_productLabe.y = 45;
			addChild(_productLabe);
			
			_getExperienceLabel.x = _getExperience.x + _getExperience.width + 5;
			_getExperienceLabel.y = _getExperience.y;
			$produceBg.addChild(_getExperienceLabel);
			
			_equipNumLabel.x = _kindOfEquip.x + _kindOfEquip.width + 5;
			_equipNumLabel.y = _kindOfEquip.y;
			$produceBg.addChild(_equipNumLabel);
		}
		//////////////按钮类型
		private var _tenMinuteBtn:LabelButton;
		private var _thirtyMinuteBtn:LabelButton;
		private var _oneHourBtn:LabelButton;
		private var _twoHourBtn:LabelButton;
		private var _eightHourBtn:LabelButton;
		private var _twentyFourHourBtn:LabelButton;
		private function setButton():void
		{
			_tenMinuteBtn = getButtonByTime(10,"分钟");
			_thirtyMinuteBtn = getButtonByTime(30,"分钟");
			_oneHourBtn = getButtonByTime(1,"小时");
			_twoHourBtn = getButtonByTime(2,"小时");
			_eightHourBtn = getButtonByTime(8,"小时");
			_twentyFourHourBtn = getButtonByTime(24,"小时");
			for(var i:uint = 0;i<buttonArr.length;i++)
			{
				(buttonArr[i] as LabelButton).x = 20 + ((buttonArr[i] as LabelButton).width + 8) * i;
				(buttonArr[i] as LabelButton).y = 10;
				addChild(buttonArr[i] as LabelButton);
				(buttonArr[i] as LabelButton).addEventListener(MouseEvent.CLICK,clickHandle);
			}
		}
		
		private function clickHandle(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case _tenMinuteBtn:
					trace(e.currentTarget.text);
					break;
				case _thirtyMinuteBtn:
					trace(e.currentTarget.text);
					break;
				case _oneHourBtn:
					trace(e.currentTarget.text);
					break;
				case _twoHourBtn:
					trace(e.currentTarget.text);
					break;
				case _eightHourBtn:
					trace(e.currentTarget.text);
					break;
				case _twentyFourHourBtn:
					trace(e.currentTarget.text);
					break;
				default:
					break;
			}
		}
		/**
		 * 
		 * @param data
		 * @param time分钟  通过分钟来计算获得经验值  以及装备件数
		 * 
		 */		
		public function changeProduceWeaponNeedUIData(data:Object,time:int = 10):void
		{
			var experienceNum:int;
			var equipNum:int;
			var $equip:EquipObj = data as EquipObj
			_kindOfEquip.background = $equip.getIcon(36.8,40.0);
			
			equipNum = time / $equip.time;
			
			_getExperienceLabel.text = "x" + experienceNum.toString();
			
			_equipNumLabel.text = "x" + equipNum.toString();
			_stoneNeedLabel.text = "x" + ($equip.needStone * equipNum).toString();
			_woodNeedLabel.text = "x" + ($equip.needWood * equipNum).toString();
			_goldNeedLabel.text = "x" + ($equip.needGold * equipNum).toString();
			_ironNeedNum.text = "x" +  ($equip.needIron * equipNum).toString();
		}
	}
}